package com.qianf.qfoffice.controller;


import com.qianf.qfoffice.entity.Grade;
import com.qianf.qfoffice.entity.Grades;
import com.qianf.qfoffice.entity.ResponseData;
import com.qianf.qfoffice.entity.Student;
import com.qianf.qfoffice.service.GradeService;
import com.qianf.qfoffice.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }


    @Autowired
    private StudentService studentService;


    /**
     * 查询 所有 班级
     * @return
     */
    @RequestMapping("/selectGrade.do")
    public ResponseData<Object> selectGradeAll(){


        List<Grade> grade = studentService.findAllGrade();

        ResponseData<Object> responseData = new ResponseData<>();

        // ######### 必须是0 才代表响应成功
        responseData.setCode(0);
        responseData.setData(grade);

        return responseData;

    }
    /**
     * 查询 所有 班级
     * @return
     */
    @RequestMapping("/insertStudent.do")
    public ResponseData insertStudent(Student student){


        int i = studentService.addStudent(student);
        ResponseData<Object> responseData = new ResponseData<>();

        if (i > 0){
            responseData.setCode(1);
            responseData.setMsg("添加学生成功");
            return responseData;
        }else {
            responseData.setCode(0);
            responseData.setMsg("添加学生失败");
            return responseData;
        }

    }
    @RequestMapping("/selectStuGrade.do")
    public ResponseData<Grades> getDepartNum () {
        ResponseData<Grades> responseData = new ResponseData<>();

        Grades gradesNum = studentService.getGradesNum();

        try {
            responseData.setCode(0);
            responseData.setData(gradesNum);
            return responseData;
        } catch (Exception e) {
            responseData.setCode(1);
            responseData.setMsg("服务异常");
        }
        return responseData;

    }

}
